package com.crm.jdy.common.handler;

import com.crm.jdy.common.myexception.MyException;
import com.crm.jdy.common.utils.api.ResultVo;
import org.apache.dubbo.config.annotation.Service;
import org.apache.ibatis.exceptions.TooManyResultsException;
import org.hibernate.validator.internal.engine.path.PathImpl;
import org.mybatis.spring.MyBatisSystemException;
import org.springframework.http.HttpStatus;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;


@ResponseBody
@ControllerAdvice
public class GlobalExceptionHandler {


    @ExceptionHandler(Exception.class)
    @ResponseBody
    String handleException(){
        return "Exception Deal!";
    }


    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(RuntimeException.class)
    @ResponseBody
    public ResultVo exceptionHandler(Exception e){
        return ResultVo.failed("运行时异常"+e.getMessage());
    }

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(MyException.class)
    @ResponseBody
    public ResultVo MyException(Exception e){
        return ResultVo.failed("自定义异常"+e.getMessage());
    }

//    org.mybatis.spring.MyBatisSystemException
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(MyBatisSystemException.class)
    @ResponseBody
    public ResultVo MyBatisSystemException(Exception e){
        return ResultVo.failed("自定义异常"+e.getMessage());
    }

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(TooManyResultsException.class)
    @ResponseBody
    public ResultVo TooManyResultsException(Exception e){
        return ResultVo.failed("自定义异常"+e.getMessage());
    }

     @ResponseStatus(HttpStatus.BAD_REQUEST)
     @ExceptionHandler(MethodArgumentNotValidException.class)
     public ResultVo Exception(MethodArgumentNotValidException e) {
           if (e instanceof MethodArgumentNotValidException) {
               MethodArgumentNotValidException  ex = (MethodArgumentNotValidException) e;
               List<ObjectError>  objectErrors = ex.getBindingResult().getAllErrors();
               if(!CollectionUtils.isEmpty(objectErrors)) {
                   StringBuilder msgBuilder = new StringBuilder();
                   for (ObjectError objectError : objectErrors) {
                       msgBuilder.append(objectError.getDefaultMessage()).append(",");
                   }
                   String errorMessage = msgBuilder.toString();
                   if (errorMessage.length() > 1) {
                       errorMessage = errorMessage.substring(0, errorMessage.length() - 1);
                   }
                   return ResultVo.failed(errorMessage);
               }
               return ResultVo.failed(e.getMessage());
          }
         return   ResultVo.failed("22222",e.getMessage());
      }

}



